//
//  NewsTalkViewController.m
//  AndCampus
//
//  Created by 史宵宵 on 2017/12/7.
//  Copyright © 2017年 史宵宵. All rights reserved.
//

#import "NewsTalkViewController.h"
#import "LeftTalkTableViewCell.h"
#import "RIghtTalklTableViewCell.h"
#import "XHInputView.h"

@interface NewsTalkViewController ()<XHInputViewDelagete>
@property (nonatomic,strong) NSMutableArray *dataArray;
@property (nonatomic, strong) XHInputView *inputViewStyleDefault;

@end

@implementation NewsTalkViewController
- (NSMutableArray *)dataArray{
    if (!_dataArray) {
        _dataArray = [NSMutableArray array];
    }
    return _dataArray;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.title = @"私信";
    self.tableView.frame = CGRectMake(0, 0, kScreenW, kScreenH - 44 - 64);
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    self.tableView.backgroundColor = KBACKGROUNDCOLOR;
    [self.tableView registerNib:[UINib nibWithNibName:NSStringFromClass([LeftTalkTableViewCell class]) bundle:nil] forCellReuseIdentifier:@"LeftTalkTableViewCell"];
    [self.tableView registerNib:[UINib nibWithNibName:NSStringFromClass([RIghtTalklTableViewCell class]) bundle:nil] forCellReuseIdentifier:@"RIghtTalklTableViewCell"];
   
    [self creatDataList];
    [self creatMessageView];
    if (self.scanNewsTalkBlock) {
        self.scanNewsTalkBlock();
    }
    
}
- (void)creatMessageView{
    UIView *footView = [[UIView alloc]init];
    footView.backgroundColor = [UIColor whiteColor];
    footView.frame = CGRectMake(0, kScreenH - 108, kScreenW, 44);
    [self.view addSubview:footView];
    
    UIButton *send = [[UIButton alloc]init];
    send.backgroundColor = [UIColor groupTableViewBackgroundColor];
    send.clipsToBounds = YES;
    send.layer.cornerRadius = 3;
    send.frame = CGRectMake(10, 4, kScreenW - 60 - 30, 34);
    [send addTarget:self action:@selector(sendMessage) forControlEvents:UIControlEventTouchUpInside];
    [footView addSubview:send];
    
    UILabel *plLabel = [[UILabel alloc]init];
    plLabel.frame = CGRectMake(10, 7, 100, 20);
    plLabel.textColor = [UIColor lightGrayColor];
    plLabel.text = @"我来说两句";
    plLabel.font = [UIFont systemFontOfSize:14];
    [send addSubview:plLabel];
    
    UIButton *sendButton = [UIButton buttonWithType:UIButtonTypeCustom];
    sendButton.frame = CGRectMake(kScreenW - 60 - 10, 5, 60, 34);
    sendButton.backgroundColor = KBACKNAVIGATIONCOLOR;
    sendButton.clipsToBounds = YES;
    sendButton.layer.cornerRadius = 3;
    [sendButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [sendButton setTitle:@"发送" forState:UIControlStateNormal];
    [sendButton addTarget:self action:@selector(sendMessage) forControlEvents:UIControlEventTouchUpInside];
    sendButton.titleLabel.font = [UIFont systemFontOfSize:15];
    [footView addSubview:sendButton];
}
- (void)sendMessage{
    __weak typeof(self) weakSelf = self;
    self.inputViewStyleDefault = [self inputViewWithStyle:InputViewStyleDefault];
    self.inputViewStyleDefault.delegate = self;
    [self.view addSubview:self.inputViewStyleDefault];
    /** 发送按钮点击事件 */
    
    self.inputViewStyleDefault.sendBlcok = ^(NSString *text) {
        [weakSelf.inputViewStyleDefault hide];//隐藏输入框
        YJNetWorkManger *yjNetWorkManger =  [YJNetWorkManger shareManager];
        NSMutableDictionary *dic = [[NSMutableDictionary alloc]init];
        [dic setValue:weakSelf.friendId forKey:@"friend_id"];   //被动接受消息的人的id
        [dic setValue:[Global getUserTokenInfo] forKey:@"token"]; //用户的token
        if ([weakSelf.newsTalkType isEqualToString:@"classmate"]) {
            [dic setValue:@"0" forKey:@"type"]; //1飞鸽传书,这里传飞鸽传书  0：同学消息
        }else{
            [dic setValue:@"1" forKey:@"type"]; //1飞鸽传书,这里传飞鸽传书  0：同学消息
        }
        
        [dic setValue:text forKey:@"context"]; //评论内容
        [yjNetWorkManger requestType:POST andURL:[JSPathUtil urlAppMessageAddNoticeContactMessage] andParmas:dic andComplition:^(id response, BOOL isuccess) {
            if (isuccess) {
                
                [Global showActivityWithView:weakSelf.view withText:@"发表留言成功"];
//                [weakSelf.tableView reloadData];
//                [weakSelf.tableView reloadSections:[NSIndexSet indexSetWithIndex:0] withRowAnimation:UITableViewRowAnimationNone];
                [weakSelf creatDataList];
                if (weakSelf.scanNewsTalkBlock) {
                    weakSelf.scanNewsTalkBlock();
                }
            }
        }];
        
    };
    [self.inputViewStyleDefault show];
}
-(XHInputView *)inputViewWithStyle:(InputViewStyle)style{
    
    XHInputView *inputView = [[XHInputView alloc] initWithStyle:style];
    //设置最大输入字数
    inputView.maxCount = 50;
    //输入框颜色
    inputView.textViewBackgroundColor = [UIColor groupTableViewBackgroundColor];
    //占位符
    inputView.placeholder = @"我来说两句";
    return inputView;
}
- (void)creatDataList{
    YJNetWorkManger *yjNetWorkManger =  [YJNetWorkManger shareManager];
    NSMutableDictionary *dic = [[NSMutableDictionary alloc]init];
    [dic setValue:[Global getUserTokenInfo] forKey:@"token"]; //用户的token
    [dic setValue:self.newsId forKey:@"id"]; //1：查询朋友互动类型 2
    [dic setValue:self.friendId forKey:@"friend_id"];
    [dic setValue:@"1" forKey:@"type"]; //1：查询朋友互动类型 2进入系统通知详情   3进入校园圈消息点赞
    [yjNetWorkManger requestType:POST andURL:[JSPathUtil urlAppMessageGetNoticeDetailById] andParmas:dic andComplition:^(id response, BOOL isuccess) {
        if (isuccess) {
            [self.dataArray removeAllObjects];
            [TalkInfo mj_setupReplacedKeyFromPropertyName:^NSDictionary *{
                return @{@"talkId" : @"id"};
            }];
            [self.dataArray addObjectsFromArray:[TalkInfo mj_objectArrayWithKeyValuesArray:response[@"list"]]];
            [self.tableView reloadData];
        }
    }];
}
- (NSInteger )numberOfSectionsInTableView:(UITableView *)tableView{
    return self.dataArray.count;
}
- (NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return 1;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    TalkInfo *talkInfo = self.dataArray[indexPath.section];
    if ([talkInfo.type isEqualToString:@"1"]) {
        RIghtTalklTableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:@"RIghtTalklTableViewCell" forIndexPath:indexPath];
        cell.backgroundColor = KBACKGROUNDCOLOR;
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        cell.talkInfo = self.dataArray[indexPath.section];
        return cell;
    }
    LeftTalkTableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:@"LeftTalkTableViewCell" forIndexPath:indexPath];
    cell.backgroundColor = KBACKGROUNDCOLOR;
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    cell.talkInfo = self.dataArray[indexPath.section];
    return cell;
   
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    TalkInfo *newsInfo = self.dataArray[indexPath.row];
    return [Global getSizeOfString:newsInfo.context maxWidth:200 maxHeight:CGFLOAT_MAX withFontSize:15.6].height + 40;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    UIView *view = [[UIView alloc]init];
    view.frame = CGRectMake(0, 0, kScreenW, 40);
    //    view.backgroundColor = KBACKGROUNDCOLOR;
    
    TalkInfo *newsInfo = self.dataArray[section];
    double timeWidth = [Global getSizeOfString:newsInfo.add_time maxWidth:kScreenW maxHeight:20 withFontSize:14].width + 10;
    UILabel *timeLabel = [[UILabel alloc]init];
    timeLabel.frame = CGRectMake((kScreenW - timeWidth)/2, 10, timeWidth, 20);
    timeLabel.backgroundColor = RGBColor(219, 219, 219);
    timeLabel.textColor = [UIColor whiteColor];
    timeLabel.textAlignment = NSTextAlignmentCenter;
    timeLabel.font = [UIFont systemFontOfSize:14];
    timeLabel.text = newsInfo.add_time;
    timeLabel.clipsToBounds = YES;
    timeLabel.layer.cornerRadius = 2;
    [view addSubview:timeLabel];
    
    return view;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 40.01f;
}
- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
    UIView *view = [[UIView alloc]init];
    view.frame = CGRectMake(0, 0, kScreenW, 5);
    //    view.backgroundColor = KBACKGROUNDCOLOR;
    
    return view;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    return 5.01f;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
